package net.oktaeder.office.simulation;

import java.util.ArrayList;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.oktaeder.office.model.Employee;
import net.oktaeder.office.model.WorkOrder;
import net.oktaeder.office.view.Mail;

/**
 * Class to handle interaction between user and work force pool
 * 
 * @author tuukka
 *
 */
public class LabourMarket {

	private static Log log = LogFactory.getLog(LabourMarket.class);
	private ArrayList<WorkOrder> orders;
	
	public LabourMarket() {
		orders = new ArrayList<WorkOrder>();
	}
	
	public void receiveMail(WorkOrder order) {
		log.info("Mail received");
		this.orders.add(order);
	}
	
	public void handleMails() {
		//TODO: implement properly
		Iterator<WorkOrder> ordersIterator;
		WorkOrder order;
		
		ordersIterator = orders.iterator();
		
		while (ordersIterator.hasNext()) {
			order = ordersIterator.next();
			if (order.getValue("action").equals("list")) {
				Iterator<Employee> iterator;
				String message;
				Employee tempEmployee;
				Mail mail;
				
				iterator = Simulation.getInstance().getWorkForce().getIterator();
				message = new String();
								
				while (iterator.hasNext()) {
					tempEmployee = iterator.next();
					message = message.concat(tempEmployee.getPIN() + ": " + tempEmployee.getName());
				}
				
				mail = new Mail();
				//TODO: this could be configurable or randomized
				mail.setSubject("Your requested labour market report");
				mail.setMessage(message);
				mail.setRecepient("player");
				mail.setSender("Labour market");
				
				Simulation.getInstance().getMailDaemon().addMail(mail);
				//TODO: handle listing of free employees
				// construct mail
				// list free employees
				// send mail to daemon
			}
		}
	}
	
	public int amountOfOrders() {
		return orders.size();
	}	
}
